// pages/school/modify/modify.js
var schoolservice = require("../../../apiservice/school/schoolservice");
var schoolId = '';
const jsonData = require("../jsondata.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    street: { new_streetid: '', new_name: '请选择', province: null, city: null, area: null },
    schoolItem: {},
    /**
     * 选项集
     */
    stageArray: jsonData.stageArray,
    stageIndex: 0,
    levelArray: jsonData.levelArray,
    levelIndex: 0,
    propertyArray: jsonData.propertyArray,
    propertyIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    schoolId = options.schoolId
    //获取修改对象
    schoolservice.getSchoolDetail(schoolId).then(function (success) {
      //console.log(success);
      that.setData({
        schoolItem: success.data,
      })
      //初始化选项集
      var stageId = parseInt(that.data.schoolItem.new_stage - 1);
      var levelId = parseInt(that.data.schoolItem.new_level - 1);
      var propertyId = parseInt(that.data.schoolItem.new_property - 1) > 2 ? 2 : parseInt(that.data.schoolItem.new_property - 1);
      var streetName = that.data.schoolItem.new_streetName;
      //console.log(stageId);
      that.setData({     
        stageIndex: stageId,
        levelIndex: levelId,
        propertyIndex: propertyId,
        ['street.new_name']: streetName,
        ['schoolItem.provinceName']: options.province,
        ['schoolItem.cityName']: options.city,
        ['schoolItem.areaName']: options.area
      })

    }).catch(function (ex) {
      console.log(ex);
    });
  },
  /***
  * 选择框赋值
  */
  bindStageChange: function (e) {
    var stageId = this.data.stageArray[e.detail.value].id;
    //console.log(stageId);
    this.setData({
      stageIndex: e.detail.value,
      ['schoolItem.new_stage']: stageId
    });
    //console.log(this.data.schoolItem);
  },
  bindLevelChange: function (e) {
    var levelId = this.data.levelArray[e.detail.value].id;
    this.setData({
      levelIndex: e.detail.value,
      ['schoolItem.new_level']: levelId
    });
  },
  bindPropertyChange: function (e) {
    var propertyId = this.data.propertyArray[e.detail.value].id;
    this.setData({
      propertyIndex: e.detail.value,
      ['schoolItem.new_property']: propertyId
    });
  },
  /***
   * 文本赋值
   * 
   */
  bindNameChange:function(e){
    //console.log(e);
    this.setData({
     ['schoolItem.new_name'] : e.detail
    });
  },
  bindAddressChange:function(e){
    this.setData({
      ['schoolItem.new_address'] : e.detail
     });
  },
  bindContactNameChange:function(e){
    this.setData({
      ['schoolItem.new_contactName'] : e.detail
     });
  },
  bindMobileChange:function(e){
    this.setData({
      ['schoolItem.new_mobile'] : e.detail
     });
     //console.log(this.data.schoolItem);
  },
  /**
   * 跳转到街道列表
   * 
   */
  toStreetListPage: function () {
    console.log("跳转到街道详细列表");
    wx.navigateTo({
      url: '../../street/list/list?type=choose'
    });

  },
  /**
   * 修改信息
   */
  onMod: function () {
    if(this.checkInfo()==false){return false;}
    var province = this.data.schoolItem.provinceName
    var city = this.data.schoolItem.cityName
    var area = this.data.schoolItem.areaName
    var schoolObj = this.data.schoolItem;
    schoolservice.modSchoolInfo(schoolObj).then(function (success) {
      console.log(success); 
      wx.showToast({
        title:'修改成功',
        icon:'success',
        duration:2000
      });;  
	  wx.navigateBack({
		  delta:1
	  })
      // wx.redirectTo({
      //   url:'../list/list?type=detail&province='+province +"&city="
      //   + city+"&area=" +area
      // });
    }).catch(function (ex) {
      console.log(ex);
      wx.showToast({
        title:'修改失败',
        icon:'none',
        duration:2000
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var streetid = this.data.street.new_streetid;
    this.setData({
      ['schoolItem.new_street'] : streetid,
      ['schoolItem.provinceName'] :  this.data.street.province,
      ['schoolItem.cityName'] : this.data.street.city,
      ['schoolItem.areaName'] : this.data.street.area
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checkInfo:function(){
    // console.log("checkinfo:");
    // console.log(this.data.schoolItem.new_name);
    // console.log(this.data.schoolItem.new_street);
    if(this.data.schoolItem.new_name==null||this.data.schoolItem.new_name==""){
      wx.showToast({
        title: '学校名称为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(this.data.schoolItem.new_street==null||this.data.schoolItem.new_street==""){
      wx.showToast({
        title: '请选择街道',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    return true;
  }
})